<?php

/**
 * @file classes/manager/form/SDTTForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class SDTTForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal SDTTs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class SDTTForm extends Form {

	/** @var $SDTTId int The ID of the SDTT being edited */
	var $SDTTId;

	/** @var $includeSDTTEditor object Additional SDTT editor to
	 *       include in assigned list for this SDTT
	 */
	var $includeSDTTEditor;

	/** @var $omitSDTTEditor object Assigned SDTT editor to omit from
	 *       assigned list for this SDTT
	 */
	var $omitSDTTEditor;

	/** @var $SDTTEditors array List of user objects representing the
	 *       available SDTT editors for this journal.
	 */
	var $SDTTEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function SDTTForm($SDTTId = null) {
		parent::Form('acctKT/SDTTForm.tpl');

		$journal =& Request::getJournal();
		$this->SDTTId = $SDTTId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'mant', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of SDTT editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->SDTTEditors =& $roleDao->getUsersByRoleId(ROLE_ID_SDTT_EDITOR, $journal->getId());
		$this->SDTTEditors =& $this->SDTTEditors->toArray();
		
	}

	/**
	 * When displaying the form, include the specified SDTT editor
	 * in the assigned list for this SDTT.
	 * @param $SDTTEditorId int
	 */
	function includeSDTTEditor($SDTTEditorId) {
		foreach ($this->SDTTEditors as $key => $junk) {
			if ($this->SDTTEditors[$key]->getId() == $SDTTEditorId) {
				$this->includeSDTTEditor =& $this->SDTTEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified SDTT editor from
	 * the assigned list for this SDTT.
	 */
	function omitSDTTEditor($SDTTEditorId) {
		foreach ($this->SDTTEditors as $key => $junk) {
			if ($this->SDTTEditors[$key]->getId() == $SDTTEditorId) {
				$this->omitSDTTEditor =& $this->SDTTEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$SDTTDao =& DAORegistry::getDAO('SDTTDAO');
		return $SDTTDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display($templateMgr) {
		$journal =& Request::getJournal();
		$templateMgr =& TemplateManager::getManager();

		$templateMgr->assign('SDTTId', $this->SDTTId);
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->SDTTId)) {
			$SDTTDao =& DAORegistry::getDAO('SDTTDAO');
			$SDTT =& $SDTTDao->getSDTT($this->SDTTId, false, false);
			if ($SDTT == null) {
				unset($this->SDTTId);
			} else {
				$this->_data = array(
					'matk' => $SDTT->getmatk(), // Localized
					'tentk' => $SDTT->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'mant', 'madv', 'dknont', 'dknovn'));
	}

	/**
	 * Save SDTT.
	 */
	function execute($nam) {

		$SDTTDao =& DAORegistry::getDAO('SDTTDAO');

		if (isset($this->SDTTId)) {
			$SDTT =& $SDTTDao->getSDTT($this->SDTTId, false, false);
		}

		if (!isset($SDTT)) {
			$SDTT = new SDTT();
		}

		$SDTT->setmatk($this->getData('matk'), null); // Localized
		$SDTT->setmant($this->getData('mant'), null); // Localized

		
		if (isset($this->SDTTId)) {
			$SDTTDao->updateSDTT($SDTT);
			$SDTTId = $SDTT->getId();

		} else {

			$SDTTId = $SDTTDao->insertSDTT($SDTT, $nam);
			
		}

	}
}

?>
